package com.zx.emaster;

import android.content.Intent;
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.zzp.basepresenter.ui.activity.BaseActivity;
import com.zzp.basepresenter.util.DateUtils;
import com.zzp.basepresenter.util.LayoutUtil;
import com.zzp.basepresenter.util.LoggerUtils;
import com.zzp.basepresenter.util.TextUtils;
import com.zzp.basepresenter.util.ToastUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends BaseActivity {

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.ll_info_count)
    LinearLayout llInfoCount;
    @BindView(R.id.iv_e_code)
    ImageView ivECode;
    @BindView(R.id.tv_risk)
    TextView tvRisk;
    @BindView(R.id.tv_trip)
    TextView tvTrip;
    @BindView(R.id.btn_scan)
    Button btnScan;
    @BindView(R.id.tv_status)
    TextView tvStatus;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_id_number)
    TextView tvIdNumber;
    @BindView(R.id.tv_back)
    TextView tvBack;

    @Override
    public int getContentView() {
        return R.layout.activity_main;
    }

    @Override
    public boolean fitWindow() {
        return false;
    }

    @Override
    public boolean softWindowAdjustPan() {
        return false;
    }

    @Override
    public void init(Bundle savedInstanceState) {
        ButterKnife.bind(this);
        tvTitle.setLayoutParams(LayoutUtil.getInstance().getRelativeLayoutMarginParams(context, tvTitle));
        tvBack.setLayoutParams(LayoutUtil.getInstance().getRelativeLayoutMarginParams(context, tvBack));
        btnScan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AppUtil.getInstance().checkScanPermissions(context);
            }
        });
        tvBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isSimpleClick()) {
                    clearData();
                }
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        String scanResult = AppUtil.getInstance().getScanResult(requestCode, resultCode, data);
        try {
            JSONObject jsonObject = new JSONObject(scanResult);
            Long times = jsonObject.getLong("times");
            LoggerUtils.getInstance().i("" + times);
            if (DateUtils.getInstance().minScale(times)) {
                JSONObject object = jsonObject.getJSONObject("userBean");
                UserBean userBean = new UserBean();
                userBean.setAge(object.getString("age"));
                userBean.setFaceImg(object.getString("faceImg"));
                userBean.setId(object.getString("id"));
                userBean.setId_number(object.getString("id_number"));
                userBean.setName(object.getString("name"));
                userBean.setRisk_level(object.getInt("risk_level"));
                JSONArray array = object.getJSONArray("trip");
                List<TripBean> list = new ArrayList<>();
                for (int i = 0; i < array.length(); i++) {
                    TripBean tripBean = new TripBean();
                    tripBean.setLevel(array.getJSONObject(i).getInt("level"));
                    tripBean.setName(array.getJSONObject(i).getString("name"));
                    tripBean.setTime(array.getJSONObject(i).getString("time"));
                    list.add(tripBean);
                }
                userBean.setTrip(list);
                userBean.setVaccine_status(object.getInt("vaccine_status"));
                initData(userBean);
            } else {
                ToastUtils.getInstance().ShortToast(context, "二维码已过期");
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    public void initData(UserBean userBean) {
        tvStatus.setText(userBean.getVaccine_status() == 0 ? getString(R.string.unvaccinated) : getString(R.string.vaccinated));
        tvName.setText(URLDecoder.decode(userBean.getName()));
        tvIdNumber.setText(String.format("%s*************%s", userBean.getId_number().substring(0, 1), userBean.getId_number().substring(userBean.getId_number().length() - 1)));
        tvRisk.setText(userBean.getRisk_level() == 1 ? "低风险" : userBean.getRisk_level() == 2 ? "中风险" : userBean.getRisk_level() == 3 ? "高风险" : "");
        String trip = "";
        for (int i = 0; i < userBean.getTrip().size(); i++) {
            trip = trip + URLDecoder.decode(userBean.getTrip().get(i).getNameWithColor()) + " → ";
        }
        String tripTitle = userBean.getRisk_level() == 1 ? getString(R.string.trip) :
                userBean.getRisk_level() == 2 ? TextUtils.getInstance().wrapperColor("#FF962F", getString(R.string.trip)) :
                        userBean.getRisk_level() == 3 ? TextUtils.getInstance().wrapperColor("#FF3011", getString(R.string.trip)) : getString(R.string.trip);
        tvTrip.setText(Html.fromHtml(tripTitle + ":" + trip.substring(0, trip.lastIndexOf(" → "))));
        ivECode.setImageResource(R.drawable.ic_head);
        switch (userBean.getRisk_level()) {
            case 1:
                ivECode.setBackground(getResources().getDrawable(R.drawable.style_64ac69_solid_layout));
                break;
            case 2:
                ivECode.setBackground(getResources().getDrawable(R.drawable.style_ff962f_solid_layout));
                break;
            case 3:
                ivECode.setBackground(getResources().getDrawable(R.drawable.style_ff3011_solid_layout));
                break;
        }
        tvRisk.setVisibility(View.VISIBLE);
        tvTrip.setVisibility(View.VISIBLE);
        ivECode.setVisibility(View.VISIBLE);
        tvBack.setVisibility(View.VISIBLE);
        btnScan.setText(R.string.continue_scan);
    }

    private void clearData() {
        tvStatus.setText(R.string.is_vaccinate);
        tvName.setText(R.string.info_by_scan);
        tvIdNumber.setText(R.string.id_numb_by_scan);
        tvRisk.setText("");
        tvTrip.setText("");
        tvRisk.setVisibility(View.GONE);
        tvTrip.setVisibility(View.GONE);
        ivECode.setVisibility(View.GONE);
        btnScan.setText(R.string.begin_scan);
    }
}